Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
How to perform "like" query with full text search in mongodb?

I know that mongo does not support $regex or like operation with full text search at the moment.It only matches the text or the phrase.But I need to perform like query search on indexed fields.So, I need help regarding the choice of multiple search frameworks or any other work around to solve this issue.

I have nested document structure. For eg:

{
"name": "John",
"phones": [{
    "phone_number": "1234",
    "is_primary": true
}, {
    "phone_number": "5678",
    "is_primary": false
}]
}

I have tried using Apache Solr but I found that it does not provide good support for nested documents. It flattens the structure and indexes the fields but when I tried to perform like query as I have discussed above, it did not work.

I am using scala, play and mongodb.It is not feasilbe for me to change the nested document structure now. What are the suitable search platforms that I could integrate with mongodb to develop a powerful search framework ? How can I go about handling this issue ? What would be the best approach in this scenario ?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

To perform like full text search you can create text index and use then. more about text index

Syntax to create text index

db.colectionName.createIndex( { fieldName: "text" } );

Here shown for single field you can also create with multiple fields. but remember that

A collection can have at most one text index.

then you can search using $text and $search

var searchText= 'some text to search';
db.collectionName.find({ $text: { $search: searchText} });

But to sequence matching or pattern matching no need to create text index can use $regex.

var searchName ='som';
db.collectionName.find({"name":{ $regex: searchName, $options: 'i' }})

for above query with $regex that will match partially . for example in your db name:'some text to search' so can return data if searchName value like some,som,ome, rex ...

for better performance can create normal index for name field

over 4 years ago · Santiago Trujillo Report

0

You could do this in SOLR but you need to define in advance the kinds of queries you will want to do and flatten the index in a creative ways so you can query and retrieve what you need. You can use multi-valued fields to store more than one value. You can store a JSON version of the entire object in a text field if your client needs to instantiate it.

For example you could store primaries and secondaries separately:

phone_number_primary: 1234,...
phone_number_secondary: 5678,...
my_json_blob: {OBJECT HERE}

then you can retrieve and query each separately. If you want to search for both you can either query both

 ( phone_number_primary:1234 OR phone_number_secondary:1234) 

or you can define an extra field that stores both numbers in your schema:

 <copyField source="phone_number_*" dest="phone_number_all" />

and then search this field.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!